# -*- coding: utf-8 -*-
"""
# @created on : 9月 2021/9/15 00:53
# @author     : kevin
# @file       : operation_response.py
# @software   : PyCharm
# @project    : auto_interface_test


@license: (C) Copyright 2019-2025, Corporation Limited.
@desc:
"""
import time

from jsonpath_rw import jsonpath, parse
from common.logger import Logger
import json
from lxml import etree


class OperationResponse:
    def __init__(self):
        self.log = Logger()

    def get_value_from_json_by_key(self, response, key):
        """
        通过表达式，在json数据中查找指定值并返回
        :param response: 返回数据
        :param key: 关键字表达式
        :return: 查找到的值
        """
        try:
            value = parse(key).find(json.loads(response))
            return [math.value for math in value][0]
        except:
            return None

    def get_value_from_xml_by_key(self, response, key):
        """
        通过xpath方式在xml文本中查找指定值
        :param response: 返回数据
        :param key: xpath表达式
        :return: 查找到的值
        """
        try:
            dom = etree.HTML(response.text)
            value = dom.xpath(key)[0]
        except:
            value = None
        finally:
            return value
